# @Time:2021/6/11 15:33
# @Author:5120195554王欣霏
# -*- coding = utf-8 -*-

import os
import pygame
from pygame.constants import QUIT
from scene.tiled_scene import TileScene as tilescene
from actor.swk import SWK
from actor.cattle import Cattle
import pytmx
from pygame.locals import *
from scene.fade_scene import FadeScene
from scene import SceneResult
from dialog.battle_dialog import BattleDialog
from actor.cattle_battle import CattleBattleStatus
from actor.earth_god import EarthGod
from scene.fade_scene import SceneStatus


class TempleScene(object):
    scene_result = SceneResult.Ongoing  # 表示场景正在运行

    def __init__(self, swk: SWK, surface: pygame.Surface):
        self.screen = surface
        self.swk_player = swk
        self.swk_pos_x = 0
        self.swk_pos_y = 0
        backtmxpath = os.path.join('../resource', 'tmx', 'temple1.tmx')
        # 加载.tmx图片
        self.tiled_scene = tilescene(backtmxpath)
        self.back_surface = self.tiled_scene.surface
        self.fade_scene = FadeScene(self.back_surface)
        self.temp_surface = pygame.Surface((800, 600))
        # self.swk_player = SWK() 这个场景的孙悟空是上个场景的 不能在这里创建
        self.obstacle_group = pygame.sprite.Group()  # 创建障碍物的精灵组
        self.monster_group = pygame.sprite.Group()  # 创建怪物的精灵组
        # 窗口位置的初始化
        self.win_pos_x = 0
        self.win_pos_y = 0
        self.dx_dy = [0, 0]  # 这个值等于孙悟空移动的值
        self.init_actor()
        self.battle_dialog = None

        self.temp_flag = False
        # self.temp_count = 0

    def init_actor(self):
        # 孙悟空的位置加载进来之后 可以算出孙悟空相对于整个图片的位置 然后调用函数计算出孙悟空相对于窗体的位置
        # 取出瓦格图片有关的数据
        for group in self.tiled_scene.tiled.tmx_data.objectgroups:
            if isinstance(group, pytmx.pytmx.TiledObjectGroup):  # 判断是不是瓦格的对象组
                if group.name == 'actor':  # 判断是不是actor对象组
                    for obj in group:  # 遍历actor对象组
                        if obj.name == 'sun':  # 判断是否有孙悟空
                            self.swk_player.set_pos(obj.x, obj.y)  # 将孙悟空全局的位置赋值给pos_x和pos_y
                            self.win_pos_x = obj.x - 400  # 将孙悟空显示在中间   窗口的大小是(800,600)
                            self.win_pos_y = obj.y - 300
                if group.name == 'obstacle':  # 判断是不是障碍物的对象组
                    for obj in group:
                        obs = pygame.sprite.Sprite()  # 为每一个障碍物都创建一个精灵对象
                        obs.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.obstacle_group.add(obs)  # 加入到障碍物的精灵组
                if group.name == 'monster':
                    for obj in group:
                        monster = Cattle(obj.x, obj.y)
                        self.monster_group.add(monster)
                if group.name == 'actor':
                    for obj in group:
                        self.earth_god = EarthGod(obj.x, obj.y)

    # 得到当前窗体的图层 并进行绘制
    def get_curr_surface(self):
        # self.win_surface = self.back_surface.subsurface(self.win_pos_x, self.win_pos_y, 800, 600)  # 每次都要重新获取win_surface
        self.win_surface = self.fade_scene.get_back_image(self.win_pos_x, self.win_pos_y)  # 获取渐变场景
        self.temp_surface.blit(self.win_surface, (0, 0))  # 绑定
        self.swk_player.draw(self.temp_surface, self.win_pos_x, self.win_pos_y)  # 孙悟空画在temp_surface上
        for monster in self.monster_group:
            monster.draw(self.temp_surface, self.win_pos_x, self.win_pos_y)

        if self.battle_dialog and not self.battle_dialog.over_show:  # battle_dialog存在 && 没有显示完
            self.battle_dialog.draw(self.temp_surface)

        if len(self.monster_group) == 0:  # 该场景的所有牛魔王全部打败
            self.earth_god.draw(self.temp_surface,self.win_pos_x,self.win_pos_y,False)
            self.earth_god.Move()

        self.screen.blit(self.temp_surface, (0, 0))

    # 循环体
    def run(self):
        clock = pygame.time.Clock()
        scene_exit = False
        # 主图层
        while not scene_exit:
            key_down = False
            self.dx_dy = [0, 0]
            for monster in self.monster_group:
                monster.Move()
            for event in pygame.event.get():
                if event.type == QUIT:
                    self.scene_result=SceneResult.Quit
                    scene_exit = True
                if event.type == KEYDOWN:
                    key_down = True
                    pressed_key = event.key
                    self.dx_dy = self.swk_player.key_move(pressed_key, key_down, self.obstacle_group)
                    if key_down == True:
                        self.win_pos_x += self.dx_dy[0]
                        self.win_pos_y += self.dx_dy[1]
                    if self.earth_god.is_stop and pressed_key==K_y:
                        #进入下一个场景
                        self.fade_scene.set_status(SceneStatus.Out)
                        self.earth_god.is_stop=False
            if self.fade_scene.get_out():
                self.scene_result=SceneResult.Next
                scene_exit=True

            # 越界判断
            if self.win_pos_x < 0:
                self.win_pos_x = 0
            if self.win_pos_y < 0:
                self.win_pos_y = 0
            if self.win_pos_x > (1999 - 800):
                self.win_pos_x = 1999 - 800
            if self.win_pos_y > (1495 - 600):
                self.win_pos_y = 1495 - 600

            self.create_battle_dialog()
            if self.battle_dialog and not self.battle_dialog.over_show:  # battle_dialog存在 && 没有显示完
                self.battle_dialog.process(key_down, pressed_key)
                self.swk_player.hp = self.battle_dialog.swk.hp  # 传回hp
                self.monster_status = self.battle_dialog.monster.status
                # if self.monster_status==CattleBattleStatus.DieOver:
                #     self.temp_count +=1
                # # print(self.temp_count)
                if self.monster_status == CattleBattleStatus.DieOver:
                    self.monster_group.remove(self.battle_dialog.monster)
                    # print(len(self.monster_group))

            if self.fade_scene.status!=SceneStatus.Out:
                collide_result = self.earth_god.collide(self.swk_player)


            self.get_curr_surface()
            pygame.display.update()
            clock.tick(15)  # 设置每次循环的帧率 防止对象层的对象移动过快

    def create_battle_dialog(self):
        collide_flag = False
        if self.battle_dialog is None:  # 当对话框没有出现过 表示没有打过 才能去打
            collide_flag = True
        elif self.battle_dialog.over_show:  # 当已经显示完了
            collide_flag = True
        if collide_flag:
            collide_list = pygame.sprite.spritecollide(self.swk_player, self.monster_group, True)  # 碰撞了之后怪物消失掉
            if collide_list:
                # self.battle_dialog = BattleDialog(self.swk_player.hp,self.monster_group)
                self.battle_dialog = BattleDialog(self.swk_player.hp)
